﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;

namespace WinFormClient
{
    public class Oracle : IDataBase
    {
        #region DataBase
        private DataTable GetDataTable(string queryString)
        {
            try
            {
                using (OracleDataAdapter adpter = new OracleDataAdapter(queryString, this._connectionString))
                {
                    DataTable dt = new DataTable();
                    adpter.Fill(dt);
                    return dt;
                }
            }
            catch
            {
                return null;
            }
        }

        private object Get(string queryString)
        {
            try
            {
                using (OracleConnection conn = new OracleConnection(this._connectionString))
                {
                    using (OracleCommand comm = new OracleCommand(queryString, conn))
                    {
                        comm.Connection.Open();
                        object rtn = comm.ExecuteScalar();
                        comm.Connection.Close();
                        return rtn;
                    }
                }
            }
            catch
            {
                return null;
            }
        }
        #endregion

        private string _connectionString;
        public string ConnectionString
        {
            get
            {
                return _connectionString;
            }
            set
            {
                _connectionString = value;
            }
        }

        //indentity
        public string Identity(string sequence)
        {
            return "SELECT " + sequence.Replace(".", "") + ".NEXTVAL AS VALUE FROM DUAL";
        }

        public string NAME
        {
            get { return "Oracle"; }
        }

        public string PRIVATE
        {
            get { return "oracleClient1.0"; }
        }

        public Position IdentityPosition
        {
            get
            {
                return Position.createStatementBefore;
            }
        }

        private static string OracleDB2Csharp(string oracleDataType)
        {
            switch (oracleDataType.ToUpper())
            {
                case "VARCHAR2": return CsharpType.STRING;
                case "VARCHAR": return CsharpType.STRING;
                case "CHAR": return CsharpType.STRING;
                case "NUMBER": return CsharpType.FLOAT_NULLABLE;
                case "INTEGER": return CsharpType.INT_NULLABLE;
                case "DATE": return CsharpType.DATETIME_NULLABLE;
                case "LONG": return CsharpType.STRING;
                case "LONG RAW": return CsharpType.STRING;
                case "RAW": return CsharpType.BYTE_ARRAY;
                case "NVARCHAR2": return CsharpType.STRING;
                case "NVARCHAR": return CsharpType.STRING;
                case "ROWID": return CsharpType.STRING;
                case "NCHAR": return CsharpType.STRING;
                case "CLOB": return CsharpType.STRING;
                case "NCLOB": return CsharpType.STRING;
                case "BLOB": return CsharpType.BYTE_ARRAY;
                case "BFILE": return CsharpType.BYTE_ARRAY;
                case "FLOAT": return CsharpType.FLOAT_NULLABLE;
                case "UROWID": return CsharpType.STRING;
                default: return CsharpType.UNKNOW;
            }
        }

        static class CsharpType
        {
            public static string STRING { get { return "string"; } }
            public static string FLOAT { get { return "float"; } }
            public static string FLOAT_NULLABLE { get { return "float?"; } }
            public static string INT { get { return "int"; } }
            public static string INT_NULLABLE { get { return "int?"; } }
            public static string BYTE_ARRAY { get { return "byte[]"; } }
            public static string DATETIME_NULLABLE { get { return "DateTime?"; } }
            public static string UNKNOW { get { return "Unknow"; } }
        }
    }
}
